<template>
  <view class="shop-page">
    <!-- 页面标题 -->
    <view class="page-header">
      <text class="page-title">商品购买</text>
      <text class="page-subtitle">精选宠物湿粮产品</text>
    </view>

    <!-- 商品列表 -->
    <view class="product-list">
      <view 
        class="product-item" 
        v-for="product in productList" 
        :key="product.id"
        @tap="goToProductDetail(product)"
      >
        <view class="product-image">
          <image :src="product.mainImage" class="product-img" mode="aspectFill"></image>
        </view>
        
        <view class="product-info">
          <text class="product-name">{{ product.productName }}</text>
          <text class="product-desc">{{ product.productDesc }}</text>
          
          <view class="product-footer">
            <view class="price-section">
              <text class="price-symbol">¥</text>
              <text class="price-current">{{ product.salePrice }}</text>
            </view>
            
            <view class="buy-button" @tap.stop="buyNow(product)">
              <text class="buy-text">立即购买</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view class="empty-state" v-if="productList.length === 0">
      <text class="empty-text">暂无商品</text>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import api from '@/utils/api'
import { BASE_URL } from '@/utils/config.js'
// 响应式数据
const productList = ref([])
const loading = ref(false)

// 加载商品列表
const loadProducts = async () => {
  loading.value = true
  try {
    const productListResp = await api.product.getProductList()
    console.log('返回的商品列表:', productListResp)
    
    // 处理API返回的数据
    if (productListResp && productListResp.data && productListResp.data.records) {
      productList.value = productListResp.data.records.map(item => ({
        id: item.id,
        productName: item.productName,
        productDesc: item.productDesc,
        salePrice: item.salePrice,
        mainImage: BASE_URL + item.mainImage,
        detailImages: item.detailImages ? item.detailImages.split(',').map(img => BASE_URL + img) : [],
        soldCount: item.soldCount,
      }))
    }
  } catch (error) {
    console.error('加载商品失败:', error)
  } finally {
    loading.value = false
  }
}

// 跳转到商品详情页
const goToProductDetail = (product) => {
  uni.navigateTo({
    url: `/pages/product/detail?id=${product.id}`
  })
}

// 立即购买
const buyNow = (product) => {
  uni.navigateTo({
    url: `/pages/product/detail?id=${product.id}&buyNow=true`
  })
}

// 生命周期
onLoad(() => {
  loadProducts()
})
</script>

<style scoped>
.shop-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #FFF8E1 0%, #FFECB3 100%);
  padding-bottom: 100px;
}

/* 页面标题 */
.page-header {
  padding: 40px 20px 20px;
  text-align: center;
  background: white;
  margin-bottom: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.page-title {
  display: block;
  font-size: 24px;
  font-weight: bold;
  color: #FFB800;
  margin-bottom: 8px;
}

.page-subtitle {
  display: block;
  font-size: 14px;
  color: #666;
}

/* 商品列表 */
.product-list {
  padding: 0 16px;
}

.product-item {
  background: white;
  border-radius: 16px;
  margin-bottom: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 184, 0, 0.2);
  transition: all 0.3s ease;
}

.product-item:active {
  transform: translateY(2px);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.product-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.product-img {
  width: 100%;
  height: 100%;
}

.product-info {
  padding: 16px;
}

.product-name {
  display: block;
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
  line-height: 1.4;
}

.product-desc {
  display: block;
  font-size: 14px;
  color: #666;
  margin-bottom: 12px;
  line-height: 1.4;
}

.product-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.price-section {
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.price-symbol {
  font-size: 14px;
  color: #FF6B6B;
  font-weight: bold;
}

.price-current {
  font-size: 20px;
  color: #FF6B6B;
  font-weight: bold;
}

.price-original {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.buy-button {
  background: linear-gradient(135deg, #FFB800 0%, #FFA000 100%);
  border-radius: 20px;
  padding: 8px 16px;
  box-shadow: 0 2px 8px rgba(255, 184, 0, 0.3);
}

.buy-text {
  font-size: 14px;
  color: white;
  font-weight: bold;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
}

.empty-text {
  font-size: 16px;
  color: #999;
}
</style>